﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.XPath;

namespace BTS10_To_BTS13_Party_Converter
{
    class AgreementHandler
    {
        public void ModifyX12(XmlNode agNode, string rootNode)
        {
            string ctrlVerNum = string.Empty;
            XPathNavigator navigator = null;
            navigator = agNode.CreateNavigator();
            navigator.MoveToChild(rootNode, Constants.namespaceDeploymentURI);
            navigator.MoveToChild("ProtocolSettings", Constants.namespaceDeploymentURI);
            navigator.MoveToChild("BatchFunctionalAck", Constants.namespaceBiztalkURI);
            if (navigator.Name == "d6p1:BatchFunctionalAck")
            {
                navigator.InsertElementAfter("d6p1", "BatchImplementationAck", Constants.namespaceBiztalkURI, "true");
                navigator.MoveToParent();
            }
            navigator.MoveToChild("GenerateLoopForValidMessagesInAck", Constants.namespaceBiztalkURI);
            if (navigator.Name == "d6p1:GenerateLoopForValidMessagesInAck")
            {
                navigator.InsertElementAfter("d6p1", "GeneratePatAK901", Constants.namespaceBiztalkURI, "true");
                navigator.MoveToParent();
            }
            navigator.MoveToChild("ControlVersionNumber", Constants.namespaceBiztalkURI);
            if (navigator.Name == "d6p1:ControlVersionNumber")
            {

                ctrlVerNum = navigator.Value;

                navigator.MoveToParent();
            }
            navigator.MoveToChild("FunctionalAckVersion", Constants.namespaceBiztalkURI);
            if (navigator.Name == "d6p1:FunctionalAckVersion")
            {
                navigator.MoveToParent();
            }
            else
            {
                navigator.MoveToChild("EnableDefaultGroupHeaders", Constants.namespaceBiztalkURI);

                if (navigator.Name == "d6p1:EnableDefaultGroupHeaders")
                {
                    navigator.InsertElementAfter("d6p1", "FunctionalAckVersion", Constants.namespaceBiztalkURI, "00401");
                    navigator.MoveToParent();
                }
            }
            navigator.MoveToChild("GroupTimeFormat", Constants.namespaceBiztalkURI);
            if (navigator.Name == "d6p1:GroupTimeFormat")
            {
                navigator.InsertElementAfter("d6p1", "ImplementationAckVersion", Constants.namespaceBiztalkURI, ctrlVerNum);
                navigator.MoveToParent();
            }
            navigator.MoveToChild("NeedFunctionalAck", Constants.namespaceBiztalkURI);
            if (navigator.Name == "d6p1:NeedFunctionalAck")
            {
                navigator.InsertElementAfter("d6p1", "NeedImplementationAck", Constants.namespaceBiztalkURI, "false");
                navigator.MoveToParent();
            }
            navigator.MoveToChild("TrailingSeparatorPolicy", Constants.namespaceBiztalkURI);
            if (navigator.Name == "d6p1:TrailingSeparatorPolicy")
            {
                navigator.InsertElementAfter("d6p1", "TrimLeadingAndTrailingSpacesAndZeroes", Constants.namespaceBiztalkURI, "false");
                navigator.MoveToParent();
            }
           
        }
        public void ModifyProtocolSettings(XmlNode x12Node)
        {
            XPathNavigator navX12ValidationOverrides = null;
            navX12ValidationOverrides = x12Node.CreateNavigator(); 
            navX12ValidationOverrides.MoveToChild("SeparatorPolicy", Constants.namespaceBiztalkURI);
            if (navX12ValidationOverrides.Name == "d6p1:SeparatorPolicy")
            {
              navX12ValidationOverrides.InsertElementAfter("d6p1", "TrimLeadingAndTrailingSpacesAndZeroes", Constants.namespaceBiztalkURI, "false");
            }
        }
    }
}
